﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.ComponentModel;

namespace AdvanceLib.UI.Controls
{
    [DefaultEvent("Click")]
    public class Button : BaseControl
    {
        #region Constructors
        public Button()
        {
            this.Animator.AddAnimation("Hover", 300);
            this.Animator.AddAnimation("Pressed", 50);

            this.Paint += new System.Windows.Forms.PaintEventHandler(Button_Paint);

            this.MouseEnter += new EventHandler(Button_MouseEnter);
            this.MouseLeave += new EventHandler(Button_MouseLeave);

            this.MouseDown += new System.Windows.Forms.MouseEventHandler(Button_MouseDown);
            this.MouseUp += new System.Windows.Forms.MouseEventHandler(Button_MouseUp);

            this.Size = new Size(160, 33);
            this.Font = new Font("Segoe UI", 9);
            this.BackColor = Color.Transparent;

            this.Caption = this.GetType().ToString();
        }
        #endregion

        #region Properties
        public string Caption { get; set; }
        #endregion

        #region Event Handlers
        void Button_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            RenderEngine.Graphics = e.Graphics;

            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;

            int borderRadius = 4;

            RenderEngine.RenderBorderRadius(Brushes.White, new Rectangle(5 + 1, 5 + 1, this.Width - 3 - 5 * 2, this.Height - 3 - 5 * 2), borderRadius);
            RenderEngine.RenderBorderRadius(
                new SolidBrush(Color.FromArgb(15 + (int)(Animator.GetValue("Hover") * (float)5), Color.CornflowerBlue)),
                new Rectangle(5, 5, this.Width - 1 - 5 * 2, this.Height - 1 - 5 * 2), borderRadius);
            
            RenderEngine.RenderBorderRadius(
                new LinearGradientBrush(
                    new Rectangle(5 + 1, 5 + 1, this.Width - 3 - 5 * 2, (this.Height - 3 - 5 * 2) / 2 + 1),
                    Color.FromArgb(230, Color.White),
                    Color.FromArgb(255 - (int)((float)255 * Animator.GetValue("Pressed")), Color.White),
                    90),
                new Rectangle(5 + 1, 5 + 1, this.Width - 3 - 5 * 2, (this.Height - 3 - 5 * 2) / 2),
                borderRadius);

            RenderEngine.RenderBorderRadius(
                new LinearGradientBrush(
                    new Rectangle(5 + 1, 5 + 1, this.Width - 3 - 5 * 2, this.Height - 3 - 5 * 2),
                    Color.FromArgb(0, Color.Black),
                    Color.FromArgb(15 - (int)(Animator.GetValue("Pressed") * (float)15), Color.Black),
                    90),
                new Rectangle(5 + 1, 5 + 1, this.Width - 3 - 5 * 2, this.Height - 3 - 5 * 2),
                borderRadius);

            RenderEngine.RenderBorderRadius(
                new LinearGradientBrush(new Rectangle(5 + 1, 5 + 1, this.Width - 3 - 5 * 2, this.Height - 3 - 5 * 2 + 1),
                    Color.FromArgb((int)(Animator.GetValue("Pressed") * (float)25), Color.Black),
                    Color.FromArgb(0, Color.CornflowerBlue),
                    90),
                new Rectangle(5 + 1, 5 + 1, this.Width - 3 - 5 * 2, this.Height - 3 - 5 * 2),
                borderRadius);

            for (int i = 0; i < 5; i++)
            {
                RenderEngine.RenderBorderRadius(
                    new Pen(Color.FromArgb(Math.Min((int)(Animator.GetValue("Hover") * (i * 8)), 255), Color.CornflowerBlue)),
                    new Rectangle(i, i, this.Width - 1 - i * 2, this.Height - 1 - i * 2), borderRadius);
            }

            StringFormat f = new StringFormat();
            f.Alignment = StringAlignment.Center;
            f.LineAlignment = StringAlignment.Center;

            e.Graphics.DrawString(this.Caption, this.Font, new SolidBrush(Color.FromArgb(250, Color.White)), new Point(this.Width / 2, this.Height / 2 + 1), f);
            e.Graphics.DrawString(
                this.Caption,
                this.Font,
                new SolidBrush(Color.FromArgb(200 + (int)(Animator.GetValue("Hover") * (float)30), Color.Black)),
                new Point(this.Width / 2, this.Height / 2), f);

            RenderEngine.RenderBorderRadius(new Pen(Color.FromArgb(65, Color.Black)), new Rectangle(5, 5, this.Width - 1 - 5 * 2, this.Height - 1 - 5 * 2), borderRadius);

            RenderEngine.RenderBorderRadius(
                new Pen(Color.FromArgb((int)((float)50 * Animator.GetValue("Hover")), Color.CornflowerBlue)),
                new Rectangle(5, 5, this.Width - 1 - 5 * 2, this.Height - 1 - 5 * 2), borderRadius);
            RenderEngine.RenderBorderRadius(
                new Pen(Color.FromArgb(255 - (int)((float)150 * Animator.GetValue("Pressed")), Color.White)),
                new Rectangle(5 + 1, 5 + 1, this.Width - 3 - 5 * 2, this.Height - 3 - 5 * 2), borderRadius);        
        }
        void Button_MouseEnter(object sender, EventArgs e)
        {
            Animator.Animate("Hover", AnimationDirection.Increment);
        }
        void Button_MouseLeave(object sender, EventArgs e)
        {
            Animator.Animate("Hover", AnimationDirection.Decrement);
        }
        void Button_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            Animator.Animate("Pressed", AnimationDirection.Increment);
        }
        void Button_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            Animator.Animate("Pressed", AnimationDirection.Decrement);
        }
        #endregion
    }
}
